package threads;

import gui.WindowMain;
import recordsDatabase.ListOfPersons;
import resources.FileCleaner;
import resources.PDFTextParser;
import resources.Populator;

public class LoaderThread extends Thread {

	

	// initialize the populator thread
	private Populator populatorThread = new Populator();
	// initialize the PDFTextparser thread
	private PDFTextParser parserThread = new PDFTextParser();
	// initialize the File Cleaner Thread
	private FileCleaner fileCleanerThread = new FileCleaner();
	private WindowMain window;

	public LoaderThread(WindowMain mw) {
		//copy the status toolbar thread!
		window=mw;
		
	}
	
	
	public void run(){
		//start the threads!
		
	}

	public boolean loadPDF(String fileName) {
		window.progressBar.setMaximum(7);
		window.progressBar.setValue(1);
		String output = "temp.txt";
		// Parses the text from a pdf file to text file.
		window.addStatusMessage(
				"Parsing text from pdf file: " + fileName);
		parserThread.go(fileName, output);
		window.progressBar.setValue(2);
		if (fileCleanerThread.clean(output)) {
			window.progressBar.setValue(3);
			window.addStatusMessage(
					"Starting to load data..");
			window.progressBar.setValue(4);
			populatorThread.loadData(output);
			window.progressBar.setValue(5);
			window.addStatusMessage("Data loaded!");
			window.progressBar.setValue(6);
			window.addStatusMessage(
					"Records loaded: "
							+ ListOfPersons.getInstance().getPersonsList()
									.size());
			window.progressBar.setValue(7);
			return true;
		} else
			window.addStatusMessage(
					"File could not be cleaned!");
		return false;

	}

	public void loadTXT(String input) {

		if (fileCleanerThread.clean(input)) {
			window.addStatusMessage(
					"Starting to load data..");
			populatorThread.loadData(input);
			window.addStatusMessage("Data loaded!");
			window.addStatusMessage(
					"Records loaded: "
							+ ListOfPersons.getInstance().getPersonsList()
									.size());
		} else
			window.addStatusMessage(
					"File could not be cleaned!");

	}

}
